/**
* @file rtcommon.h in loosely_couple_h7
* @author rebeater
* @comment tool funcitons for real time application
* Create on 3/24/21 3:38 PM
* @version 1.0
**/

//
// Created by rebeater on 3/24/21.
//

#ifndef LOOSELY_COUPLE_H7_RTCOMMON_H
#define LOOSELY_COUPLE_H7_RTCOMMON_H
#include "global_defines.h"
#include <stdio.h>
#include <math.h>
//char buffer[8];
#include "global_defines.h"
#include <cmsis_os.h>
#define delay(ms) osDelay(ms)

#if IMU_DEVICE == IMU_ADIS16460
#include "adis16460.h"
#define IMU_RAW ImuRawAdi
#define RAW_TO_IMU(X,Y) convert_adi_to_double(X,Y)
#define INIT_IMU_DEVICE()  adis16460_init()
#define READ_IMU_FIFO(X,ID) adis16460_burst_read(X)
#elif IMU_DEVICE == IMU_KYIMU102
#include "bdkyimu.h"
#define IMU_RAW ImuRawKy
#define RAW_TO_IMU(X,Y) ConvertKyToDouble(X,Y)
#define INIT_IMU_DEVICE() KyImu102A0Initial()
#define READ_IMU_FIFO(X,ID) KyImu102A0ReadRaw(X)
#elif IMU_DEVICE == IMU_ICM20602
#include "icm20602.h"
#define IMU_RAW ImuRawIcm
#if ENABLE_MULTI_IMUS==1
#define INIT_IMU_DEVICE() Icm20602MultiInit(IMU_MODE_CAR, IMU_RATE_50Hz)
#else
#define INIT_IMU_DEVICE() icm20602_init_spi(IMU_MODE_CAR, IMU_RATE_50Hz,icm20602_id)
#endif
#define RAW_TO_IMU(X,Y) convert_icm_to_double(X,Y)
#define READ_IMU_FIFO(X,ID) icm20602_read_fifo(X,ID)
#elif IMU_DEVICE == IMU_STIM300
#include <stim300.h>
#define RAW_TO_IMU(X,Y) stim300_convert_to_imu(X,Y)
#define INIT_IMU_DEVICE() Stim300UartInit()
#else
#error("IMU Device NOT SUPPORTED!")
#endif
#if GNSS_RCV == GNSS_RCV_MXT906B
//#include "nmea.h"
#define PARSE_GNSS(X, Y) DecodeMultiMsgs((const char *)X,Y)
#define GNSS_UART_INSTANCE USART2
#elif GNSS_RCV == GNSS_RCV_UB482
//#include "nmea.h"
//#include "GnssParseRaw.h"
#define PARSE_GNSS(X, Y) ParseRawBin((const char *)X,Y)
#define GNSS_UART_INSTANCE UART5
#endif
#define MAJOR_VERSION 3
#define MINOR_VERSION 2
#define BUILD_VERSION 14



extern char Version[];
extern const char *Description;
extern char GnssDeviceName[];
extern char Comments[];
extern char ImuName[];
extern char ImuFileNameFormat[];
extern DateTime date_time;
double GetCurrentGpst();

void printDouble(double d, uint8_t n);

void sprintDouble(char *buffer, double d, uint8_t n);

void printOutput(NavOutput *out);

void sprintOutput(char *buffer,short week,int gpst, NavOutput *out);

uint32_t crc_checksum(const uint8_t *indata, int num_bytes);

void gpst_to_utc(int week, double gpst,DateTime *time);
int day_from_gps0(const int year, const int mon, const int day);
typedef enum {
    Info,
    Warning,
    Error,
    Fetal,
} TraceLevel;


typedef struct {
  IMU_RAW imu_;/*8*5*/
  GnssData gnss_;/* 8*8*/
  VelocityRaw vel_; /* 8 * 2*/
  NavOutput rst_;/**/
} RawData;

/**
 * 日志打印函数
 * @param level 日志等级
 * @param fmt  format
 * @param ...
 * @return 02
 */
int trace(TraceLevel level, const char *fmt, ...) __attribute__((format(printf,2,3)));

#endif //LOOSELY_COUPLE_H7_RTCOMMON_H
